<?php
class ControllerSaleCashup extends Controller
{
    public function index()
    {
        $this->document->setTitle("结算账单管理");
        $this->load->model('sale/cashup');

        $this->getList();
    }

    public function insert() {
        $this->document->setTitle("结算账单管理");

        $this->data['tab_general'] = "添加结算账单";

        $this->load->model('sale/cashup');

//        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->model_sale_cashup->addCashup($this->request->post);

            $this->session->data['success'] = "成功： 您已成功添加结算账单！";

            $this->redirect($this->url->link('sale/cashup', 'token=' . $this->session->data['token'], 'SSL'));
        }
        $this->getForm();
    }
    public function update() {

        $this->document->setTitle("结算账单管理");
        $this->data['tab_general'] = "编辑结算账单";

        $this->load->model('sale/cashup');

//        if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
        if (($this->request->server['REQUEST_METHOD'] == 'POST')) {
            $this->model_sale_cashup->editCashup($this->request->get['id'], $this->request->post);

            $this->session->data['success'] = "成功： 您已成功更新结算账单！";

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getForm();
    }

    public function delete() {
        $this->document->setTitle("结算账单管理");


        $this->load->model('sale/cashup');

        if (isset($this->request->post['selected'])) {
            foreach ($this->request->post['selected'] as $manufacturer_id) {
                $this->model_sale_cashup->deleteCashup($manufacturer_id);
            }
            $this->session->data['success'] = "成功： 您已成功删除结算账单！";

//            $this->session->data['success'] = $this->language->get('text_success');

            $url = '';

            if (isset($this->request->get['sort'])) {
                $url .= '&sort=' . $this->request->get['sort'];
            }

            if (isset($this->request->get['order'])) {
                $url .= '&order=' . $this->request->get['order'];
            }

            if (isset($this->request->get['page'])) {
                $url .= '&page=' . $this->request->get['page'];
            }

            $this->redirect($this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url, 'SSL'));
        }

        $this->getList();
    }

    private function getList()
    {
        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 't.id';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'ASC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => "结算账单管理",
            'href' => $this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        $this->data['insert'] = $this->url->link('sale/cashup/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        $this->data['delete'] = $this->url->link('sale/cashup/delete', 'token=' . $this->session->data['token'] . $url, 'SSL');

        $this->data['attributes'] = array();

        $data = array(
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $attribute_total = $this->model_sale_cashup->getTotalCashups();

        $results = $this->model_sale_cashup->getCashups($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_edit'),
                'href' => $this->url->link('sale/cashup/update', 'token=' . $this->session->data['token'] . '&id=' . $result['id'] . $url, 'SSL')
            );
            if($result['status'] == 'ACTIVE'){
                $status = "已结算";
            }elseif($result['status'] == 'REGISTER'){
                $status = "申请中";
            }elseif($result['status'] == 'DELETE'){
                $status = "删除";
            }
            $this->data['cashups'][] = array(
                'id' => $result['id'],
                'account' => $result['account'],
                'register_time' => $result['register_time'],
                'account_time' => $result['account_time'],
                'name' => $result['firstname'],
                'status' => $status,
                'selected' => isset($this->request->post['selected']) && in_array($result['id'], $this->request->post['selected']),
                'action' => $action
            );
        }

        $this->data['heading_title'] = "结算账单管理";

        $this->data['text_no_results'] = $this->language->get('text_no_results');

//         $this->data['column_name'] = $this->language->get('column_name');
//         $this->data['column_attribute_group'] = $this->language->get('column_attribute_group');
//         $this->data['column_sort_order'] = $this->language->get('column_sort_order');
//         $this->data['column_action'] = $this->language->get('column_action');

//         $this->data['button_insert'] = $this->language->get('button_insert');
//         $this->data['button_delete'] = $this->language->get('button_delete');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

//         $this->data['sort_name'] = $this->url->link('extension/telephone', 'token=' . $this->session->data['token'] . '&sort=ad.name' . $url, 'SSL');
//         $this->data['sort_attribute_group'] = $this->url->link('extension/telephone', 'token=' . $this->session->data['token'] . '&sort=attribute_group' . $url, 'SSL');
//         $this->data['sort_sort_order'] = $this->url->link('extension/telephone', 'token=' . $this->session->data['token'] . '&sort=a.sort_order' . $url, 'SSL');

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $attribute_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $this->data['pagination'] = $pagination->render();

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'sale/cashup_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }
    private function getForm() {
//        $this->data['heading_title'] = "结算账单管理";

//        $this->data['text_enabled'] = $this->language->get('text_enabled');
//        $this->data['text_disabled'] = $this->language->get('text_disabled');
//        $this->data['text_default'] = $this->language->get('text_default');
//        $this->data['text_image_manager'] = $this->language->get('text_image_manager');
//        $this->data['text_browse'] = $this->language->get('text_browse');
//        $this->data['text_clear'] = $this->language->get('text_clear');
//        $this->data['text_percent'] = $this->language->get('text_percent');
//        $this->data['text_amount'] = $this->language->get('text_amount');
//
//        $this->data['entry_name'] = $this->language->get('entry_name');
//        $this->data['entry_store'] = $this->language->get('entry_store');
//        $this->data['entry_keyword'] = $this->language->get('entry_keyword');
//        $this->data['entry_image'] = $this->language->get('entry_image');
//        $this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
//        $this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
//
//        $this->data['button_save'] = $this->language->get('button_save');
//        $this->data['button_cancel'] = $this->language->get('button_cancel');

//        $this->data['tab_general'] = $this->language->get('tab_general');

        if (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->error['name'])) {
            $this->data['error_name'] = $this->error['name'];
        } else {
            $this->data['error_name'] = '';
        }

        $url = '';

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text'      => $this->language->get('text_home'),
            'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text'      => "结算账单管理",
            'href'      => $this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        if (!isset($this->request->get['id'])) {
            $this->data['action'] = $this->url->link('sale/cashup/insert', 'token=' . $this->session->data['token'] . $url, 'SSL');
        } else {
            $this->data['action'] = $this->url->link('sale/cashup/update', 'token=' . $this->session->data['token'] . '&id=' . $this->request->get['id'] . $url, 'SSL');
        }

        $this->data['cancel'] = $this->url->link('sale/cashup', 'token=' . $this->session->data['token'] . $url, 'SSL');

        if (isset($this->request->get['id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
            $manufacturer_info = $this->model_sale_cashup->getCashup($this->request->get['id']);
        }

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->request->post['account'])) {
            $this->data['account'] = $this->request->post['account'];
        } elseif (!empty($manufacturer_info)) {
            $this->data['account'] = $manufacturer_info['account'];
        } else {
            $this->data['account'] = '';
        }
        if (isset($this->request->post['status'])) {
            $this->data['status'] = $this->request->post['status'];
        } elseif (!empty($manufacturer_info)) {
            $this->data['status'] = $manufacturer_info['status'];
        } else {
            $this->data['status'] = '';
        }

//        $this->load->model('setting/store');
//
//        $this->data['stores'] = $this->model_setting_store->getStores();
//
//        if (isset($this->request->post['manufacturer_store'])) {
//            $this->data['manufacturer_store'] = $this->request->post['manufacturer_store'];
//        } elseif (isset($this->request->get['manufacturer_id'])) {
//            $this->data['manufacturer_store'] = $this->model_catalog_manufacturer->getManufacturerStores($this->request->get['manufacturer_id']);
//        } else {
//            $this->data['manufacturer_store'] = array(0);
//        }
//
//        if (isset($this->request->post['keyword'])) {
//            $this->data['keyword'] = $this->request->post['keyword'];
//        } elseif (!empty($manufacturer_info)) {
//            $this->data['keyword'] = $manufacturer_info['keyword'];
//        } else {
//            $this->data['keyword'] = '';
//        }
//
//        if (isset($this->request->post['image'])) {
//            $this->data['image'] = $this->request->post['image'];
//        } elseif (!empty($manufacturer_info)) {
//            $this->data['image'] = $manufacturer_info['image'];
//        } else {
//            $this->data['image'] = '';
//        }
//
//        $this->load->model('tool/image');
//
//        if (isset($this->request->post['image']) && file_exists(DIR_IMAGE . $this->request->post['image'])) {
//            $this->data['thumb'] = $this->model_tool_image->resize($this->request->post['image'], 100, 100);
//        } elseif (!empty($manufacturer_info) && $manufacturer_info['image'] && file_exists(DIR_IMAGE . $manufacturer_info['image'])) {
//            $this->data['thumb'] = $this->model_tool_image->resize($manufacturer_info['image'], 100, 100);
//        } else {
//            $this->data['thumb'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
//        }
//
//        $this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);
//
//        if (isset($this->request->post['sort_order'])) {
//            $this->data['sort_order'] = $this->request->post['sort_order'];
//        } elseif (!empty($manufacturer_info)) {
//            $this->data['sort_order'] = $manufacturer_info['sort_order'];
//        } else {
//            $this->data['sort_order'] = '';
//        }

        $this->template = 'sale/cashup_form.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    private function validateForm() {
        if (!$this->user->hasPermission('modify', 'sale/cashup')) {
            $this->error['warning'] = "警告： 您没有权限更改结算账单！";
        }

//        if (utf8_strlen($this->request->post['telephone']) != 11) {
//            $this->error['name'] = "结算账单必须是11位";
//        }

//        if ($this->error && !isset($this->error['warning'])) {
//            $this->error['warning'] = "警告： 相关数据未键入！";
//        }
        if (!$this->error) {
            return true;
        } else {
            return false;
        }

    }
}

?>
